<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2004-2015 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 张盛翔
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;

class IndexController extends CommonController{

	public function index(){
		$news_group=$this->m();
		$news_detail=$this->detail();
		$acount=$news_detail->count();
		$count=$news_group->count();
		$list=$news_group->select();
		if($news_group->where(array('news_group_id' => $_GET['id']))->delete()){
			header("refresh:1;url=".__ACTION__);
		};
		$this->assign('acount',$acount);
		$this->assign('count',$count);
		$this->assign('list',$list);
		$this->display();
	}

	public function Llist(){
		$this->index();	
	}

	public function edit(){
		$news_group=$this->m();
		$data=$news_group->where(array('news_group_id' => $_GET['id']))->find();
		$this->assign('data',$data);
		if(!empty($_GET['sub'])){
			$id=$_GET['id'];
			if($_GET['sub']='submit1'){
				header("refresh:1;url=".__APP__);
			}
			unset($_GET['sub']);
			unset($_GET['id']);
			if($news_group->where(array('news_group_id' => $id))->save($_GET)){
				header("refresh:2;url=".__APP__);
  				
				$this->succ("编辑成功,两秒后回到首页");
			}
		}	
		$this->display();
	}

	public function add(){
		$news_group=$this->m();
		$name=$news_group->field('news_group_name')->where(array('news_group_name'=>$_GET['news_group_name']))->find();
		if(!empty($_GET['sub'])){
			unset($_GET['sub']);
			if(isset($name)){
				$this->succ("栏目已经存在!");	
			}else{
				$news_group->add($_GET);
				$this->succ("添加栏目成功,你可以继续在本页面添加栏目!&nbsp;&nbsp;&nbsp;&nbsp;<a href=".__APP__.">回到首页</a>");
			}	
		}
		$this->display();	
	}

	public function articlelist(){
		$news_detail=$this->detail(); 
		$news_group=$this->m();
		if ($news_detail->where(array('id' => $_GET['id']))->delete()) {
			header("refresh:1;url=".__ACTION__);
		}
		
		if(!empty($_POST['sub'])){
			if(count($_POST['id'])>1){
				$where='id in('.implode(',',$_POST['id']).')';
			}else{
				$where= 'id='.$_POST['id']['0'];		
			}
			$list=$news_detail->where($where)->delete();
			if(!$list==false){
				$this->succ("成功删除".$list."条数据!");
			}
		}

		$count= $news_detail->count();
		$Page = new  \Think\Page($count, 15);
		$this->page($count,15);
		$show = $Page->show();
		$list = $news_group->join('tp_newborn_detail ON tp_newborn_list.news_group_id = tp_newborn_detail.news_group_id')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display(); 
	}

	public function article(){
		$news_detail=$this->detail(); 
		$news_group=$this->m();
		$news_detail->where(array('id' => $_GET['aid']))->delete();
		$count= $news_detail->where( array('news_group_id' => $_GET['id']))->count();
		$Page = new  \Think\Page($count, 15);
		$this->page($count,15);
		$show = $Page->show();
		$list = $news_group->join('tp_newborn_detail ON tp_newborn_list.news_group_id = tp_newborn_detail.news_group_id')->where( array('tp_newborn_list.news_group_id' => $_GET['id']))->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display(); 
	}

	public function arcadd(){
		$news_group=$this->m();
		$news_detail=$this->detail(); 
		$list=$news_group->select();
		$this->assign('list',$list);
		if(isset($_POST['sub'])){
			unset($_POST['sub']);
			$_POST['create_time']=date("Y-m-d H:i:s", time());
			$_POST['update_time']=date("Y-m-d H:i:s", time());
			if($news_detail->add($_POST)){
				$this->succ("添加文章成功,你可以继续在本页面添加文章!&nbsp;&nbsp;<a href=".__APP__."/Admin/Index/article/id/".$_POST['news_group_id'].">回到栏目</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=".__APP__.">回到首页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=".__APP__."/Admin/Index/articlelist>回到文章列表</a>");	
			}	
		}
		
		$this->display();	
	}

	public function arcedit(){
		$news_group=$this->m();
		$news_detail=$this->detail();
		$data=$news_group->field('news_group_id,news_group_name')->select();
		$list=$news_detail->join('tp_newborn_list ON tp_newborn_detail.news_group_id=tp_newborn_list.news_group_id')->where(array('tp_newborn_detail.id' =>$_GET['aid']))->find();
		$this->assign('data',$data);
		$this->assign('list',$list);	
		if(!empty($_POST['sub'])){
			unset($_POST['sub']);
			$_POST['update_time']=date("Y-m-d H:i:s", time());
			if($news_detail->where(array('id' =>$_POST['id']))->save($_POST)){	
				$this->succ("编辑文章成功 &nbsp;&nbsp;<a href=".__APP__."/Admin/Index/article/id/".$_POST['news_group_id'].">回到栏目</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=".__APP__.">回到首页</a>");
			}
		}	
		$this->display();	
	}

}

